/**
 * Created by Administrator on 2017/5/2 0002.
 */
$(document).ready(function () {

    var _student_id = false;

    //判断所输入学号有没有被使用
    $("#student_id").focus(function () {
        $("#notice_student_id").text("例如：41511P01");
        $("#notice_student_id").css("color", "black");
        $("#student_id").blur(function () {
            var student_id = $("#student_id").val();
            $.get("/StudentServlet?action=student_idCanUsed&student_id=" + student_id, function (date) {
                var message = $(date).find("message").text();
                if (message == "该学号可以使用") {
                    if (student_id.length == 8) {
                        _student_id = true;
                        $("#notice_student_id").text(message);
                        $("#notice_student_id").css("color", "green");
                    } else {
                        _student_id=false;
                        $("#notice_student_id").text("例如：41511P01");
                        $("#notice_student_id").css("color", "black");
                    }
                } else {
                    $("#notice_student_id").text(message);
                    $("#notice_student_id").css("color", "red");
                }

            });
        });
    });

    //ajax得到宿舍列表
    $.get("/StudentServlet?action=getDormitory_id", function (date) {
        $(date).find("dormitory_id").each(function () {
            var dormitory_id = $(this).text();
            $("#student_dormitory_id").append("<option class='' id=''>" + dormitory_id + "</option>");
        })
    });

    //提交表单,添加学生
        $("#submit").click(function () {
            if (_student_id) {
            var student_id = $("#student_id").val();
            var student_name = $("#student_name").val();
            var student_sex = $(":radio").val();
            var student_age = $("#student_age").val();
            var student_state = $("#student_state").val();
            var student_phone = $("#student_phone").val();
            var student_qq = $("#student_qq").val();
            var student_address = $("#student_address").val();
            var student_nation = $("#student_nation").val();
            var student_dormitory_id = $("#student_dormitory_id").val().replace(/#/, '%23');
            // alert(student_id+" "+student_name+" "+student_sex+" "+student_age+" "+student_state+" "+student_phone+" "+student_qq+" "+student_address+" "+student_nation+" "+student_dormitory_id);
            $.get("/StudentServlet?action=addStudent&student_id=" + student_id +
                "&student_name=" + student_name +
                "&student_sex=" + student_sex +
                "&student_age=" + student_age +
                "&student_state=" + student_state +
                "&student_phone=" + student_phone +
                "&student_qq=" + student_qq +
                "&student_address=" + student_address +
                "&student_nation=" + student_nation +
                "&student_dormitory_id=" + student_dormitory_id, function (date) {
                var alert_message = $(date).find("alert_message").text();
                alert(alert_message);
            });
            }
        });

    $("#CssAddStudent").attr("class","active");
});